package history.study.dynamic_programming;

// 509. 斐波那契数

/**
 *  1. dp[i] 为第i个斐波那契数列的值
 *  2. 递推公式：dp[i] = dp[i-1]+dp[i-2]
 *  3. dp[0] = 0, dp[1] = 1
 *  4. 从前往后进行遍历
 *  5. dp[2] = dp[1]+dp[0] = 1
 *     dp[3] = dp[1]+dp[2] = 2
 */
public class LeetCode_509 {
    public static void main(String[] args) {
        fib(8);
    }
    public static int fib(int n) {
        if (n < 0) return -1;
        if (n == 0 || n == 1) return n;
        int []state = new int[n+1];
        state[0] = 0;
        state[1] = 1;
        for (int i = 2; i <= n; i++) {
            state[i] = state[i-1] + state[i-2];
            System.out.println(i+":"+state[i]);
        }
        return state[n];
    }
}
